<?php
  $path = (isset($data['phieu'])) ? 'vat_tu/phieu_vat_tu/sua_phieu/xuatdec/'.$data['phieu']['ma_phieu'] : 'vat_tu/de_c/xuat_de_c';
   $class = 'xuat-de-c';
  $i = 0;

?>
<form method="post" action="<?php echo base_path() . $path ?>" id="xuat-de-c" name="xdc" class="xuat-de-c">
  <div class="form-item form-text-wrapper nha-cung-cap-wrapper">
    <label for="txt-nha-cung-cap">Nhà cung cấp</label>
    <input id="txt-nha-cung-cap" value="<?php echo ($_POST && $_POST['nha_cung_cap']) ? $_POST['nha_cung_cap'] : ((isset($data['phieu'])) ? ($data['phieu']['la_khach_hang'] == 1 ? $data['phieu']['ref'] : $data['phieu']['ref'].' ['.$data['phieu']['ma_ref'].']') : '') ?>"
           name="nha_cung_cap" type="text"
           class="form-text form-autocomplete text txt-nha-cung-cap <?php echo ($_POST && (string) $_POST['error']['key'] == 'nha_cung_cap')
             ? 'error' : '' ?>" autocomplete="OFF"/>
    <input class="autocomplete" type="hidden" id="txt-nha-cung-cap-autocomplete"
           value="<?php echo base_path() . 'nha_cung_cap/autocomplete' ?>" disabled="disabled"/>

    <div class="clear"></div>
  </div>
  <div class="form-item form-text-wrapper la-khach-hang-wrapper">
    <label for="chk-la-khach-hang">Là khách lẻ</label>
    <input id="chk-la-khach-hang" <?php echo (($_POST && $_POST['la_khach_hang'] == 1) || (isset($data['phieu']) && $data['phieu']['la_khach_hang'] == 1)) ? 'checked="checked"' : '' ?>
           value="1" name="la_khach_hang" type="checkbox" class="form-checkbox checkbox chk-la-khach-hang" />
  </div>
  <div class="clear"></div>
  <fieldset>
    <legend>Nhập số lượng đề C</legend>
    <div id="form-item-wrapper">
      <?php if ($_POST && isset($_POST['de_c'])) { ?>
      <?php foreach ($_POST['de_c'] as $key => $de_c) { ?>
        <?php $i++; ?>
        <div class="form-item-row">
          <div class="form-item form-select-wrapper">
            <label for="scl-de-c-<?php echo $i ?>">Loại đề C</label>
            <select name="de_c[]" id="scl-de-c-<?php echo $i ?>" class="form-text text scl-de-c <?php echo (isset($_POST['error']['de_c']) && $_POST['error']['key'] == $key) ? 'error' : '' ?>">
              <?php foreach ($data['gia_de_c'] as $gia_de_c ) { ?>
                <option value="<?php echo $gia_de_c->ma_de_c ?>" <?php echo ($gia_de_c->ma_de_c == $de_c ? 'selected="selected"' : '') ?>><?php echo $gia_de_c->ten_de_c.' - ' . number_format($gia_de_c->gia_tien) ?></option>
              <?php } ?>
            </select>
          </div>
          <div class="form-item">
            <input id="field-remove-submit-<?php echo $i ?>" type="button" value="X"
                   class="field-remove-submit form-submit"/>
          </div>
          <div class="form-item form-text-wrapper">
            <label for="txt-so-luong-<?php echo $i ?>">Số lượng</label>
            <input id="txt-so-luong-<?php echo $i ?>" value="<?php echo $_POST['so_luong'][$key] ?>" name="so_luong[]"
                   type="text"
                   class="form-text text txt-so-luong <?php echo (isset($_POST['error']['so_luong']) && $_POST['error']['key'] == $key)
                     ? 'error' : '' ?>" autocomplete="OFF"/>
          </div>

          <div class="form-item form-text-wrapper">
            <label for="txt-so-luong-vung-<?php echo $i ?>">Số vung</label>
            <input id="txt-so-luong-vung-<?php echo $i ?>" value="<?php echo $_POST['so_luong_vung'][$key] ?>" name="so_luong_vung[]"
                   type="text"
                   class="form-text text txt-so-luong-vung <?php echo (isset($_POST['error']['so_luong_vung']) && $_POST['error']['key'] == $key)
                     ? 'error' : '' ?>" autocomplete="OFF"/>
          </div>

          <div class="form-item form-text-wrapper">
            <label for="txt-so-luong-than-<?php echo $i ?>">Số thân</label>
            <input id="txt-so-luong-than-<?php echo $i ?>" value="<?php echo $_POST['so_luong_than'][$key] ?>" name="so_luong_than[]"
                   type="text"
                   class="form-text text txt-so-luong-than <?php echo (isset($_POST['error']['so_luong_than']) && $_POST['error']['key'] == $key)
                     ? 'error' : '' ?>" autocomplete="OFF"/>
          </div>

          <div class="form-item form-text-wrapper">
            <label for="txt-so-luong-day-<?php echo $i ?>">Số đáy</label>
            <input id="txt-so-luong-day-<?php echo $i ?>" value="<?php echo $_POST['so_luong_day'][$key] ?>" name="so_luong_day[]"
                   type="text"
                   class="form-text text txt-so-luong-day <?php echo (isset($_POST['error']['so_luong_day']) && $_POST['error']['key'] == $key)
                     ? 'error' : '' ?>" autocomplete="OFF"/>
          </div>
          <div class="clear"></div>
        </div>
        <?php } ?>

      <?php } else if(isset($data['chi_tiet'])){ ?>
      <?php foreach ($data['chi_tiet'] as $key => $de_c) { ?>
        <?php
        $sl_than = $de_c->sl_than;
        $sl_vung = $de_c->sl_vung;
        $sl_day = $de_c->sl_day;
        $so_luong = $de_c->so_luong;
          $i++;
        ?>
        <div class="form-item-row">
          <div class="form-item form-select-wrapper">
            <label for="scl-de-c-<?php echo $i ?>">Loại đề C</label>
            <select name="de_c[]" id="scl-de-c-<?php echo $i ?>" class="form-text text scl-de-c <?php echo (isset($_POST['error']['de_c']) && $_POST['error']['key'] == $key) ? 'error' : '' ?>">
              <?php foreach ($data['gia_de_c'] as $gia_de_c ) { ?>
                <option value="<?php echo $gia_de_c->ma_de_c ?>" <?php echo ($gia_de_c->ma_de_c == $de_c->ma_de_c ? 'selected="selected"' : '') ?>><?php echo $gia_de_c->ten_de_c.' - ' . number_format($gia_de_c->gia_tien) ?></option>
              <?php } ?>
            </select>
          </div>
          <div class="form-item">
            <input id="field-remove-submit-<?php echo $i ?>" type="button" value="X"
                   class="field-remove-submit form-submit"/>
          </div>
          <div class="form-item form-text-wrapper">
            <label for="txt-so-luong-<?php echo $i ?>">Số lượng</label>
            <input id="txt-so-luong-<?php echo $i ?>" value="<?php echo $so_luong ?>" name="so_luong[]"
                   type="text"
                   class="form-text text txt-so-luong <?php echo (isset($_POST['error']['so_luong']) && $_POST['error']['key'] == $key)
                     ? 'error' : '' ?>" autocomplete="OFF"/>
          </div>

          <div class="form-item form-text-wrapper">
            <label for="txt-so-luong-vung-<?php echo $i ?>">Số vung</label>
            <input id="txt-so-luong-vung-<?php echo $i ?>" value="<?php echo $sl_vung ?>" name="so_luong_vung[]"
                   type="text"
                   class="form-text text txt-so-luong-vung <?php echo (isset($_POST['error']['so_luong_vung']) && $_POST['error']['key'] == $key)
                     ? 'error' : '' ?>" autocomplete="OFF"/>
          </div>

          <div class="form-item form-text-wrapper">
            <label for="txt-so-luong-than-<?php echo $i ?>">Số thân</label>
            <input id="txt-so-luong-than-<?php echo $i ?>" value="<?php echo $sl_than ?>" name="so_luong_than[]"
                   type="text"
                   class="form-text text txt-so-luong-than <?php echo (isset($_POST['error']['so_luong_than']) && $_POST['error']['key'] == $key)
                     ? 'error' : '' ?>" autocomplete="OFF"/>
          </div>

          <div class="form-item form-text-wrapper">
            <label for="txt-so-luong-day-<?php echo $i ?>">Số đáy</label>
            <input id="txt-so-luong-day-<?php echo $i ?>" value="<?php echo $sl_day ?>" name="so_luong_day[]"
                   type="text"
                   class="form-text text txt-so-luong-day <?php echo (isset($_POST['error']['so_luong_day']) && $_POST['error']['key'] == $key)
                     ? 'error' : '' ?>" autocomplete="OFF"/>
          </div>
          <div class="clear"></div>
        </div>
        <?php } ?>
      <?php } else { ?>
      <?php $i++; ?>
        <div class="form-item-row">
          <div class="form-item form-select-wrapper">
            <label for="scl-de-c-<?php echo $i ?>">Loại đề C</label>
            <select name="de_c[]" id="scl-de-c-<?php echo $i ?>" class="form-text text scl-de-c">
              <?php foreach ($data['gia_de_c'] as $gia_de_c ) { ?>
                <option value="<?php echo $gia_de_c->ma_de_c ?>"><?php echo $gia_de_c->ten_de_c.' - ' . number_format($gia_de_c->gia_tien) ?></option>
              <?php } ?>
            </select>
          </div>
          <div class="form-item">
            <input id="field-remove-submit-<?php echo $i ?>" type="button" value="X"
                   class="field-remove-submit form-submit"/>
          </div>
          <div class="form-item form-text-wrapper">
            <label for="txt-so-luong-<?php echo $i ?>">Số lượng</label>
            <input id="txt-so-luong-<?php echo $i ?>" value="" name="so_luong[]"
                   type="text"
                   class="form-text text txt-so-luong" autocomplete="OFF"/>
          </div>

          <div class="form-item form-text-wrapper">
            <label for="txt-so-luong-vung-<?php echo $i ?>">Số vung</label>
            <input id="txt-so-luong-vung-<?php echo $i ?>" value="" name="so_luong_vung[]"
                   type="text"
                   class="form-text text txt-so-luong-vung" autocomplete="OFF"/>
          </div>

          <div class="form-item form-text-wrapper">
            <label for="txt-so-luong-than-<?php echo $i ?>">Số thân</label>
            <input id="txt-so-luong-than-<?php echo $i ?>" value="" name="so_luong_than[]"
                   type="text"
                   class="form-text text txt-so-luong-than" autocomplete="OFF"/>
          </div>

          <div class="form-item form-text-wrapper">
            <label for="txt-so-luong-day-<?php echo $i ?>">Số đáy</label>
            <input id="txt-so-luong-day-<?php echo $i ?>" value="" name="so_luong_day[]"
                   type="text"
                   class="form-text text txt-so-luong-day" autocomplete="OFF"/>
          </div>
          <div class="clear"></div>
        </div>
      <?php } ?>
    </div>
    <div id="add-more-wrapper">
      <input id="add-more-de-c" type="button" value="Thêm đề C" class="field-add-more-submit form-submit"/>
    </div>
  </fieldset>
  <div class="form-item form-textarea-wrapper">
    <label for="ghi-chu">Ghi chú</label>
    <textarea id="ghi-chu" name="ghi_chu"
              class="form-textarea <?php echo ($_POST && (string) $_POST['error']['key'] == 'ghi_chu') ? 'error'
                : '' ?>" cols="60" rows="5"><?php echo ($_POST) ? $_POST['ghi_chu'] : (isset($data['phieu']) ? $data['phieu']['ghi_chu'] : '') ?></textarea>

    <div class="description">Nhập tối đa 500 ký tự. Số lượng hiện thời: <span style="color: red;"
                                                                              id="ghi-chu-length">0</span></div>
  </div>
  <?php if($data && isset($data['phieu'])){?>
  <div class="form-item form-type-textfield form-item-date">
    <label for="edit-date">Ngày nhập </label>
   <input type="text" class="pickadate" maxlength="128" size="60" value="<?php echo ($_POST) ? $_POST['date'] : ($data ? date('Y-m-d', $data['phieu']['thoi_gian']) : '') ?>" name="date" id="edit-date" readonly="readonly" width="200px">
  </div>

  <?php }?>
  <div id="submit-wrapper">
    <?php foreach ($data['gia_de_c'] as $gia_de_c) { ?>
      <input type="hidden" id="id-de-c-hidden-<?php echo $gia_de_c->id ?>" name="id_de_c_hidden[<?php echo $gia_de_c->ma_de_c ?>]" class="id-de-c-hidden" value="<?php echo $gia_de_c->id ?>" />
      <input type="hidden" id="ma-de-c-hidden-<?php echo $gia_de_c->id ?>" name="ma_de_c_hidden[<?php echo $gia_de_c->ma_de_c ?>]" class="ma-de-c-hidden" value="<?php echo $gia_de_c->ma_de_c ?>" />
      <input type="hidden" id="ten-de-c-hidden-<?php echo $gia_de_c->id ?>" name="ten_de_c_hidden[<?php echo $gia_de_c->ma_de_c ?>]" class="ten-de-c-hidden" value="<?php echo $gia_de_c->ten_de_c ?>" />
      <input type="hidden" id="don-gia-de-c-hidden-<?php echo $gia_de_c->id ?>" name="don_gia_de_c_hidden[<?php echo $gia_de_c->ma_de_c ?>]" class="don-gia-de-c-hidden" value="<?php echo $gia_de_c->gia_tien ?>" />
    <?php } ?>
    <input type="hidden" name="ma_phieu" value="<?php echo (isset($data['phieu']['ma_phieu'])) ? $data['phieu']['ma_phieu'] : ''  ?>"/>
    <input type="hidden" name="type" value="<?php echo (isset($data['phieu'])) ? $data['type'] : ''  ?>"/>
    <input type="hidden" id="total-post" value="<?php echo $i  ?>" disabled="disabled"/>
    <input id="submit" type="submit" onclick="return check_nhap_de_c();" value="Nhập dữ liệu" class="form-submit"/>
<!--    <input id="submit" type="submit" value="Nhập dữ liệu" class="form-submit" style="display: none;"/>-->
  </div>
</form>